package com.dotstone.chipism.ui.security.me;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.BaseActivity;
import com.dotstone.chipism.ui.security.fragment.DeviceCamera2Fragment;
import com.dotstone.chipism.ui.security.fragment.DeviceCameraFragment;
import com.dotstone.chipism.ui.security.fragment.DeviceSettingFragment;
import com.dotstone.chipism.ui.security.fragment.DeviceVideoFragment;

import java.util.ArrayList;
import java.util.List;

public class DeviceCameraMainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private ViewPager vpMain ;
    private List<Fragment> fragmentList;
    private DeviceCamera2Fragment camereFargment;
    private Fragment videoFargment;
    private Fragment settingFargment;
    private RadioGroup rgMenu;
    public LinearLayout llMenu;
    public TextView tvCancel;
    public TextView tvSelect;
    public TextView tvDelete;

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_device_camera_main;
    }

    @Override
    public void initView(View view) {
        vpMain = $(R.id.vp_device_camera);
        rgMenu = $(R.id.rg_menu);
        llMenu = (LinearLayout) findViewById(R.id.ll_menu);
        tvCancel = (TextView) findViewById(R.id.tv_cancel);
        tvSelect = (TextView) findViewById(R.id.tv_select);
        tvDelete = (TextView)findViewById(R.id.tv_delete);
    }

    @Override
    public void setListener() {
        rgMenu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_camera:
                        vpMain.setCurrentItem(0,false);
                        break;
                    case R.id.rb_video:
                        vpMain.setCurrentItem(1,false);
                        break;
                    case R.id.rb_setting:
                        vpMain.setCurrentItem(2,false);
                        break;
                }
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {
        fragmentList = new ArrayList<>();
        camereFargment = new DeviceCamera2Fragment();
        videoFargment = new DeviceVideoFragment();
        settingFargment = new DeviceSettingFragment();
        fragmentList.add(camereFargment);
        fragmentList.add(videoFargment);
        fragmentList.add(settingFargment);

        vpMain.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        vpMain.setCurrentItem(position,false);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter{


        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        //   super.destroyItem(container, position, object);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
//          super.destroyItem(container, position, object);
        }
    }

    @Override
    public void onBackPressed() {
        // 如果当前是横屏，返回时先回到竖屏
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            return;
        }

        finish();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 检测屏幕的方向：纵向或横向
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // 当前为横屏， 在此处添加额外的处理代码
            camereFargment.showAsLandscape();
            rgMenu.setVisibility(View.GONE);
        }
        else if(getResources().getConfiguration().orientation
                ==Configuration.ORIENTATION_PORTRAIT) {
            // 当前为竖屏， 在此处添加额外的处理代码
            camereFargment.showAsPortrait();
            rgMenu.setVisibility(View.VISIBLE);
        }

        super.onConfigurationChanged(newConfig);
    }


    private void switchOrientation() {
        // 横竖屏切换
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
            // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}
